function [im, colorTable] = colortable(p, blocksize, edgesize)

%% configure
if nargin < 1; p = 5; end
if nargin < 2; blocksize = 50; end
if nargin < 3; edgesize = 5; end

%% create color table
nColor = p^3;
colorTable = de2bi(0:nColor-1, 3, p) / (p-1);
colorTable(:, [1 3]) = colorTable(:, [3 1]);
colorTable = uint8(255 * colorTable);
antiColor = anticolor(colorTable);

%% run: draw colorTable
w = p * blocksize;
im = zeros([w p*w+(p-1)*edgesize 3], 'uint8');

x = 1; y = 1; index = 1;
while index <= nColor
    color = colorTable(index, :);
    im = insertShape(im, 'FilledRectangle', [x y blocksize, blocksize], ...
        'Color', color, 'Opacity', 1);
    
    im = insertText(im, [x y], sprintf('%d\n%d\n%d', color), ...
        'TextColor', antiColor(index,:), 'BoxOpacity', 0);
    
    index = index + 1;
    y = y + blocksize;
    if y >= w
        y = 1;
        x = x + blocksize;
        if mod(index, p*p) == 1, x = x + edgesize; end
    end 
end

%% result
%imwrite(im, sprintf('color%d.png', p));
if nargout < 1
    clear im
    imtool(im)
end

